var RegNumber = url.templateArgs["RegNumber"];
var RegGroup = url.templateArgs["RegGroup"];
var RegTitle = url.templateArgs["RegTitle"];
var RegNote = url.templateArgs["RegNote"];
var Documents 	= url.templateArgs["Attachments"];
var Executor = url.templateArgs['Executor'];
var Mode = url.templateArgs["Mode"];

var site = "sakym-personalo-klausimais-registravimas";
var datalistRef = "32bbf642-154b-4bd9-8f04-ccf6873ae9f4";

var referenceType = "node";
var data = new Date();

function returnNode(node) { 
	var reference = ["workspace", "SpacesStore", node];
	var foundNode = search.findNode(referenceType, reference);
	return foundNode;
}

var DocArray = Documents.split("|");
var personaloList = companyhome.childByNamePath("Sites/"+site+"/dataLists/"+datalistRef); // nuoroda i data list

var iras = personaloList.createNode(null, "rcc:personaloList");
iras.properties["rcc:perRegGroup"] = RegGroup;
iras.properties["rcc:perRegData"]  = data;
iras.properties["rcc:perRegTitle"] 	= RegTitle;
iras.properties["rcc:perRegNote"] = RegNote;
iras.properties["rcc:perRegNumber"] =	RegNumber;
iras.save();

if (Executor != "no" && Executor != "" && Executor != null && Executor != undefined) {
    iras.createAssociation(returnNode(Executor),"rcc:perAssignedTo");
} else {
}

iras.createAssociation(person,"rcc:perRegistrator");

if (Mode == "multiple") {
	for (var i = 0; i < DocArray.length; i++) {
		var reference = ["workspace", "SpacesStore", DocArray[i]];
		var foundNode = search.findNode(referenceType, reference);
		if (foundNode.properties["rcc:dokTip"] == 1057) {
			iras.createAssociation(returnNode(DocArray[i]),"rcc:perAttachments");
		}
	}
} else {
	for (var i = 0; i < DocArray.length; i++) {
	   iras.createAssociation(returnNode(DocArray[i]),"rcc:perAttachments");
	}
}

if (iras.nodeRef != null) {
    status.code = 200;
	model.message = "success";
} else {
	status.redirect = true;
	status.code = 404;
	status.message = "error";
}